package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.interceptor.LoginRequire;
import cn.wolfcode.luowowo.interceptor.ResolverUserInfo;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.redis.srevice.IUserRedisSrevice;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.service.impl.StrategyServiceImpl;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("travels")
public class TravelsController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    //攻略分类
    @Autowired
    private StrategyServiceImpl strategyService;
    /*游记业务层对象*/
    @Autowired
    private ITravelService travelService;
    
    @Autowired
    private ITravelCommentService travelCommentService;
    /**
     * 根据目的地的id查询目的地关联的游记
     * @param qo 目的地id
     * @return  返回的是分页后的游记数据
     */
    @GetMapping("/query")
    public Object query(TravelQuery qo){
        Page<Travel> query = travelService.query(qo);
        
        return JsonResult.success(query);
    }
    

    /**
     * 目的地下面点击量排名前三的游记
     */
    @GetMapping("/strategies/viewnumTop3")
    public  Object strategiesViewnumTop3(String destId){
        List<Strategy> strategies = strategyService.selectViewnumTop3(destId);
        return JsonResult.success(strategies);
    }
    
    /**
     * 编辑保存界面
     * @param id
     * @return
     */
    @LoginRequire
    @GetMapping("/input")
    public Object input(String id){
        //前端要的是一个Map的数组 一个是TV 一个是dests tv 是游记信息 编辑用的 dests是目的地信息 不管编辑还是保存都是需要的
        Map<String, Object> map = travelService.selectTvAndDests(id);
        return JsonResult.success(map );
    }
    
    
    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);
        
        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    
    //保存数据的方法
    @LoginRequire
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,@ResolverUserInfo UserInfo userInfo){
        //因为前端在这个保存后跳转的是用户的游记界面 所有这里是需要用户进行登录后才能正常跳转的
        //登录的信息是token请求头 是视图层的方法逻辑 放在业务层就违反了ssm分层的理念 同时也不算是耦合
        if (userInfo.getId() != null){
            //为游记添加一个用户id
            travel.setUserId(userInfo.getId());
        }
        //使用增加编辑保功能
        travelService.saveOrUpdate(travel);
        
        return JsonResult.success(travel.getId());
    }
    
    //私人的游记界面
    @GetMapping("/detail")
    public Object detail(String id,@ResolverUserInfo UserInfo userInfo){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }
    
    //查询游记数据不分页
    @GetMapping("/comments")
    public Object comments( String travelId){
        List<TravelComment> commentByTravel = travelCommentService.getCommentByTravel(travelId);
        return JsonResult.success(commentByTravel);
    }
    
    //添加评论的方法
    @LoginRequire
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment,@ResolverUserInfo UserInfo userInfo){
        //将登录用户的信息添加进游记评论中
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
    
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }
    
}
